package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.Map.Entry;

import javax.swing.ImageIcon;

import main.Coord;
import main.TDMap;
import main.Tickable;
import runner.Enemy;
import towers.Projectile;
import towers.Tower;

public class Guimap extends Component implements Observer, Tickable {
	
//	private Map<Class, ImageIcon> iconlist;
	private ImageIcon[][] map;
	private ImageIcon icon;
	private TDMap tdmap;
	private List<Coord> path;
	public static Color PATH_COLOR = new Color(0x00, 0x00, 0xFF);
	public static int TILE_WIDTH=30,TILE_HEIGHT=30;
	public static int MAP_WIDTH=40, MAP_HEIGHT=20;
	private Set<Enemy> enemies;
	
	public Guimap(TDMap tdmap, Set<Enemy> enemies) {
		this.tdmap = tdmap;
		
		this.enemies = enemies;
		
		setIcon();
		setMap();
		
		this.setPreferredSize(new Dimension(MAP_WIDTH*TILE_WIDTH, MAP_HEIGHT*TILE_HEIGHT));
	}

	private void setMap() {
		map = new ImageIcon[MAP_WIDTH][MAP_WIDTH];
		for (int i=0; i<map.length; i++) {
			for (int j=0; j<map[0].length; j++) {
				try {
					map[i][j] = new ImageIcon(new File("t.png").toURI().toURL());
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void setIcon() {
		try {
			icon = new ImageIcon(new File("t.png").toURI().toURL());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		if (icon.getIconHeight() == -1) {
			System.out.println("image icon not found //guimap");
		}
	}
	
	@Override
	public void paint(Graphics g) {
		paintMap(g);
		paintTowers(g);
//		paintPath(g);
		paintEnemies(g);
		paintProjectiles(g);
	}
	private void paintProjectiles(Graphics g) {
		Set<Projectile> proj = tdmap.getProjectiles();
		g.setColor(Color.CYAN);
		for (Projectile p: proj) {
			g.fillOval(p.getPos().x, p.getPos().y, 3, 3);
		}
	}
	private void paintEnemies(Graphics g) {
		for (Enemy e: enemies) {
			ImageIcon icon = e.getTemplate().icon;
			g.drawImage(icon.getImage(), e.getPos().x-icon.getIconWidth()/2, e.getPos().y-icon.getIconHeight()/2, null);
		}
	}

	private void paintPath(Graphics g) {
		if (path != null) {
			g.setColor(PATH_COLOR);
			for (int i=0; i<path.size()-1; ++i) {
				g.drawLine(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y);
			}
		}
	}
	private void paintMap(Graphics g) {
		for (int i=0; i<MAP_WIDTH; i++) {
			for (int j=0; j<MAP_HEIGHT; j++) {
				g.drawImage(map[i][j].getImage(), i*icon.getIconWidth(), j*icon.getIconHeight(), null);
			}
		}
	}
	private void paintTowers(Graphics g) {
		Map<Coord, Tower> towers = tdmap.getTowerMap();
		for (Entry<Coord, Tower> e: towers.entrySet()) {
			Coord c = e.getKey().clone();
			c.convert(30, 30);
			g.drawImage(e.getValue().getTemplate().icon.getImage(), c.x-TILE_WIDTH/2, c.y-TILE_HEIGHT/2, null);
		}
	}

	@Override
	public void update(Observable obs, Object dis) {
		TDMap tdmap = (TDMap)obs;
		Map<Coord, Tower> towers = tdmap.getTowerMap();
		for (Coord c: towers.keySet()) {
//			map[c.x][c.y] = towers.get(c).getTemplate().icon; 
		}
		
		List<Coord> tpath = tdmap.getPath();
		path = new ArrayList<Coord>();
		for (Coord c: tpath) {
			path.add(new Coord(c.x*TILE_WIDTH+TILE_WIDTH/2, c.y*TILE_HEIGHT+TILE_HEIGHT/2));
		}
		repaint();
	}

	@Override
	public void tick() {
		repaint();
	}
	
	
}
